/**
*	内部邮件页面
*
*/

define(['jquery','init','jqueryValidate','uploadfiles','ckeditor','deptUserSelect','groupSelect',
			'bsButton','bsButtonEvent','bsMsgServ','formServ','bsUtil','codeDef','datagridServ'
			,'moment','bootstrap-datetimepicker','bootstrap-datetimepicker.zh-CN','dateUtil','oaMainList'],
    	function($,init,validate,uploadfiles,ckeditor,deptUserSelect,groupSelect,
    				bsButton,bsButtonEvent,bsMsgServ,formServ,bsUtil,codeDef,datagridServ
    				,moment,datetimepicker,datetimepickerLang,dateUtil,oaMainList) {
		
    	//页面根节点
    	var rootid = 'inner-mail',
    	
    	//缺省模块分类代码
    	current_bizcode = 'recivebox',
    	
    	//模块代码
    	moduCode = 'innerMail',
    	//查看页面根节点
    	viewRootid = 'mail-list',
    	
    	//detail 按钮容器
    	detail_btn_container = rootid + ' .btn-container',
    	
    	//验证公用属性
    	ruleBase = {
			errorElement : 'span',  
            errorClass : 'js-required-field',  
            focusInvalid : true
    	}
    	,
    	//ckeditor id
		ck_id = 'lob_contents',
		
		selectedRecord,
		
    	//页面定义
    	pageConfig = {
    		listUrl : '/'+init.getSite()+'/mailInfo/query/getStatusListDomain.action',
    		editUrl	: '/mailInfo/query/editPage.action',
    		sendUrl : '/'+init.getSite()+'/mailInfo/send.action',
    		saveUrl : '/'+init.getSite()+'/mailInfo/save.action',
    		delStatusUrl: '/'+init.getSite()+'/mailInfo/updateDelStatus.action',
    		delUrl	: '/'+init.getSite()+'/mailInfo/del.action',
    		updateUrl	: '/'+init.getSite()+'/mailInfo/updateReadStatus.action',
    		badgeUrl: '/'+init.getSite()+'/mailInfo/query/getBadge.action',
    		uploadListurl	: '/'+init.getSite()+'/upload/query/getInitListDomain.action',
    		contentUrl		: '/'+init.getSite()+'/mailDetail/getOne.action'
    	},
    	//当前业务字段验证规则
    	ruleConfig
    		,
    	//业务列表-表单容器对象
    	list_c = ' .list-detail',
    	
    	//业务编码定义
    	bizConfig = {
    		mailInfo	: 'mailInfo',
    		mailGroup	: 'mailGroup',
    		mailUser	: 'mailUser'
    	}
    	,
    	//邮件状态编码,null:草稿，1:未读，2:已读，3:发送，4:回收站
    	statusConfig ={
    		draft	: '',
    		recived : '1',
    		readed	: '2',
    		sended	: '3',
    		deleted : '4'
    	},    	
    	
    	

    	//当前模块配置
    	current_config={
    		bizcode	: null,
    		status	: statusConfig.recived
    	},
    	
    	
    	//当前选中行
    	curent_selectRow,
    	
    	//额外数据
    	external_data = {}
    	
    	
    	
		var private_method = {
    		
			/**
			 * 附加子表数据回调函数
			 * @param _options.field	属性名称
			 * @param _options.value	属性值
			 */
			setExternalData:function(_options){
				if(_options && _options.field){
					external_data[_options.field]=_options.value
				}
			}
			,
    	/**
    	 * 初始化侧边栏导航
    	 */
    	initNav:function(){
    		var _param = {
    			url		: pageConfig.badgeUrl,			
    			dataType: 'json',
    			success : function(data){
//    				if(window.console){
//    					console.log('menu data: ' + data);
//    				}
    				var _el = $('#' + rootid + ' #left-nav')
    				if(_el.length==1){
    					
    					//设定导航栏高度
    					var _navCss = {
    						"overflow"  : "auto",
    						"overflow-x": "hidden", 
    						"height"    : init.getGridHeight()+'px'} 
    					$(_el).css(_navCss);
    					
    					//设置侧边栏badge数值    	
    					bsUtil.updateMailBadge();
    					
    					
    					//初始化按钮
    					private_method.redrawBtn({
							bizcode:current_bizcode,
							btnType:'list',
							bizType:'flow',
							container: detail_btn_container
						})
						
						private_method.btn_listenner();
						//初始化列表
    					private_method.initList({module:current_bizcode});
    				}
    			}
    		}
    		$.ajax(_param)
    	},
    	
    	/**
    	 * 刷新邮件数量
    	 * @param _options.data	刷新数据
    	 * @param _options.evt	按钮事件
    	 */
    	refreshBadge:function(_options){
    		if(_options && _options.data){
    			var v = _options.data
    			
    			var ba = ['recived','draft','gabage']
    			
    			$.each(ba,function(i,o){
    				var _class,_badgeEl;
	    			_class = '#'+rootid + ' .badge-'+o+'-notify' ;  			
		    		_badgeEl = $(_class)
	    			if(v[o]){		    			
		    			_badgeEl.html(v[o]);
	    			}else{
	    				_badgeEl.empty();
	    			}
    			})    			
    			
    		}
    	}
    	,
    	/**
    	 * 初始化列表
    	 * @param _options.module	模块代码
    	 * @param _options.ajax		server端数据访问方法
    	 * @param _options.paging	分页标志
    	 * @param _options.columns	缺省列
    	 */
    	initList:function(_options){
    		
    		 var _param ={
    		 	height       : init.getGridHeight(),
    		 	rownumbers   : true,
				animate      : true,									
				striped      : true,
				border       : true,
				//singleSelect:false,
				idField      : 'id',								
				fitColumns   : true,
				checkOnSelect: true,					
				selectOnCheck: true,
				pagination   : true,
				url          : pageConfig.listUrl,
				columns      : [[
					{field:'ck',checkbox:true},					        
					{field:'name',title:'邮件主题',width:100,halign:'center'},        
					{field:'apply_deptname',title:'科室',width:50,halign:'center'} ,
					{field:'apply_name',title:'人员',width:50,halign:'center'} ,
					{field:'apply_date',title:'时间',width:50,halign:'center'}
					
				]],
				onDblClickRow: private_method.dblClick
    		 }
    		 var qryparam = {}
    		 if(_options && _options.module){
    		 	switch(_options.module){
    		 		//收件箱
    		 		case 'unreadbox':
    		 			_param.columns = [[
    		 				{field:'ck',checkbox:true},					        
							{field:'name',title:'邮件主题',width:100,halign:'center'},        
							{field:'sendDeptName',title:'发件人科室',width:50,halign:'center'} ,
							{field:'sendName',title:'发件人姓名',width:50,halign:'center'} ,
							{field:'createTime',title:'时间',width:50,halign:'center'}
    		 			]]
    		 			qryparam = {status:1};
    		 			break;
    		 		//收件箱
    		 		case 'recivebox':
    		 			_param.columns = [[
    		 				{field:'ck',checkbox:true},					        
							{field:'name',title:'邮件主题',width:100,halign:'center'},        
							{field:'sendDeptName',title:'发件人科室',width:40,halign:'center'} ,
							{field:'sendName',title:'发件人姓名',width:40,halign:'center'} ,
							{field:'createTime',title:'时间',width:50,halign:'center'},
							{field:'status',title:'状态',width:30,halign:'center',
								formatter: function(value,row,index){
									if (row.isRead==1){
										return '未读';
									} else {
										return '已读';
									}
								}
							}
    		 			]]
    		 			
    		 			_param.rowStyler= function(index,row){
							if (row.status==3 && row.isRead==1){
								return 'color:blue;font-weight:bold;'; // return inline style
								// the function can return predefined css class and inline style
								// return {class:'r1', style:{'color:#fff'}};	
							}
						}

    		 			qryparam = {status:3};
    		 			break;
    		 			
    		 		//发件箱
    		 		case 'sendbox':
    		 			_param.columns = [[
    		 				{field:'ck',checkbox:true},					        
							{field:'name',title:'邮件主题',width:100,halign:'center'},        
							{field:'reciveGroupNames',title:'收件群组',width:50,halign:'center'} ,
							{field:'reciveUserNames',title:'收件人姓名',width:50,halign:'center'} ,
							{field:'createTime',title:'时间',width:50,halign:'center'}
    		 			]]
    		 			qryparam = {status:4};
    		 			break;
    		 		
    		 		//草稿箱
    		 		case 'draftbox':
    		 			_param.columns = [[
    		 				{field:'ck',checkbox:true},					        
							{field:'name',title:'邮件主题',width:100,halign:'center'},        
							{field:'reciveGroupNames',title:'收件群组',width:50,halign:'center'} ,
							{field:'reciveUserNames',title:'收件人姓名',width:50,halign:'center'} ,
							{field:'createTime',title:'时间',width:50,halign:'center'}
    		 			]]    
    		 			qryparam = {status:-1};
    		 			break;
    		 		//回收站
    		 		case 'gabagde':
    		 			_param.columns = [[
    		 				{field:'ck',checkbox:true},					        
							{field:'name',title:'邮件主题',width:100,halign:'center'}, 						
							{field:'createTime',title:'时间',width:50,halign:'center'}
    		 			]]    
    		 			qryparam = {status:5};
    		 			break;
    		 		default:
    		 			_param.columns = [[
    		 				{field:'ck',checkbox:true},					        
							{field:'name',title:'邮件主题',width:100,halign:'center'},        
							{field:'sendDeptName',title:'发件人科室',width:50,halign:'center'} ,
							{field:'sendName',title:'发件人姓名',width:50,halign:'center'} ,
							{field:'createTime',title:'时间',width:50,halign:'center'}
    		 			]]
    		 			qryparam = {status:3};
    		 			break;
    		 	}
    		 }
    		 //判断参数是否为空
    		
    		 _param.queryParams = {condition:qryparam}
    		 
    		 var dg = $('#' + rootid + ' .dg-list')
    		 if(dg.length>0){
    		 	dg.datagrid(_param)
    		 }
    		
    	}
    	,
    	/**
    	 * 双击事件
    	 */
    	dblClick:function(index,data){
    		var dg = $('#' + rootid + ' .dg-list')
    		dg.datagrid('clearSelections');
    		dg.datagrid('selectRow',index);
    		var _btn = $('#' + rootid + ' [btn-type="listEditReplace"]');
    		
    		//缓存选中行
    		selectedRecord = data;
    		
    		if(_btn.length>0)
    			_btn.trigger('click');
    		else{
    			btnEvent.listEditReplace();
    		}
    		
    		//收件箱未读邮件
    		if(data && data.isRead==='1' && data.status==='3'){
    			$.ajax({
    				url	: pageConfig.updateUrl,
    				data: {id:data.id},
    				success:function(){
    					bsUtil.updateMailBadge();
    				}
    				,
    				error:function(){
    					bsMsgServ.errorSlide(codeDef.getConfig({type:'load',code:'error'}))
    				}
    			})
    		}
    	}
    	,
    	/**
    	 * 初始化
    	 */
    	initEdit:function(_options){
    		
    		//清理遗留数据
    		
    		//绑定按钮事件，redraw函数完成 
    		
    		//初始化表单元素
    		private_method.initForm();
    		
    		//初始化部门、人员及时间信息
    		private_method.initDeptUser();    		
    		
    		//获取字段验证规则,显示必填项
    		//private_method.getFieldConfig();
    		
    		
    		//初始化附件
    		private_method.initUpload();
    		
    		//private_method.initArchive();
    		
    		private_method.initCkeditor(selectedRecord);
    		
    		//绑定人员及分组选择
    		var _s = $('#'+rootid + ' .btn-select')
    		if(_s.length>0){
    			_s.unbind('click');
    			_s.bind('click',function(e){
    				init.applyBtnEvt({btnEvt:e,btnConfig:selectEvent})
    			})    			
    		}
    		
    	}
    	,
    	/**
    	 * 按钮事件定义
    	 */
    	btn_listenner:function(){
    		var _btn = $('#'+rootid +' a.left-nav-btn')
    		if(_btn.length>0){
    			_btn.unbind('click');
    			_btn.bind('click',private_method.navClick)
    		}
    	}
    	,
    	
    	initDeptUser:function(){
    		var usr = init.getCurrentUser();
					
			if(usr){
				//初始化部门信息
				var _elid = $('#'+rootid + ' .main-form #apply_deptid')
				var _elname = $('#'+rootid + ' .main-form #apply_deptname')
				var _val
				if(_elname.length==1){
					_val = _elname.val();
					if(!_val){
						_elname.val(usr.deptName);
						_elid.val(usr.deptid);
					}
				}
				
				//初始化人员信息
				_elid = $('#'+rootid + ' .main-form #apply_id')
				_elname = $('#'+rootid + ' .main-form #apply_name')
				var _val
				if(_elname.length==1){
					_val = _elname.val();
					if(!_val){
						_elname.val(usr.name);
						_elid.val(usr.id);
					}
				}
				
							
				
				//初始化时间字段
				var _elDate = $('#'+rootid + ' .main-form .single-time')
				if(_elDate.length>=1){
					var _param ={
							language:  'zh-CN',
							format: "yyyy-mm-dd hh:ii:ss",
							autoclose: true,
							minView: "month",
							maxView: "decade",
							todayBtn: true,
							pickerPosition: "bottom-left"
						}
					_elDate.datetimepicker(_param);
//					var _elVal = _elDate.val();
//					if(!_elVal){
//						_elDate.val(dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'}))	
//					}
				}
				
			}
    	}
    	,
    	/**
    	 * 初始化签名组件
    	 */
    	initSign:function(){
    		var _el = $('#'+rootid + ' .main-form .sign-control')
    			
    		if(_el.length>0){
    			_el.unbind('click');
    			_el.bind('click',private_method.signClick)
    		}
    	}
    	,
    	
    	signClick:function(e){
    		var _el = $(e.currentTarget);
    		var _timeEl
    		
    		//判断当前字段
    		if(_el.length==1){
    			
    			var _ro = _el.attr('readonly');
    			if(!(typeof(_ro)=="undefined")){
    				return ;	
    			}
    			//查找当前字段父节点
    			var _parent = _el.closest('.row-border');
    			
    			if(_parent.length==1){
	    			//赋值用户名及id
	    			var usr = init.getCurrentUser();
	    			
	    			//查找用户名字段
	    			var _uel = $(_parent).find('.sign-user');
	    			if(_uel.length==1){
	    				_uel.val(usr.name);
	    				
	    				var _id = _uel.attr('id');
	    				//查找用户id字段
		    			if(_id){
		    				var _uid = _id.substr(0,_id.length-5)+'_id';
		    				var _idEl = $('#'+rootid + ' .main-form #'+_uid);
		    				if(_idEl.length==1){
		    					_idEl.val(usr.id);	
		    				}
		    			}
	    			}  			
    			
    				//查找签名时间字段
    				var _tel =$(_parent).find('.sign-time');
    				
    				//判断签名时间字段是否存在
    				if(_tel.length==1){    					    						
						$(_tel).val(dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'}))    					
    				}
    			}
    		}
    	}
    	,
    	/**
    	 * 业务类型点击事件
    	 */
    	navClick:function(e){
    		
    		//选中状态 class 调整
    		$('#' + rootid + ' #left-nav li').removeClass('active');
    		$(e.currentTarget).closest('li').addClass('active');
    		
    		//清除缓存子表数据
    		external_data = {};
    		
    		//获取选中对象类型
    		current_bizcode = init.getNavType(e);
    		if(window.console){
				console.log('module: ' + current_bizcode);
			}
    		
    		//获取容器对象
			var _c =$('#' + rootid +list_c)
    		
			//刷新容器对象，变更数据
			if(_c){
				//清除原有内容
				_c.empty();
				
				var _html = '<table class="dg-list" cellspacing="0" width="100%"></table>';
				_c.append(_html);
			}			
			
			private_method.redrawBtn({
				bizcode:current_bizcode,
				btnType:'list',
				bizType:'flow',
				container: detail_btn_container
			})
			
			if(current_bizcode && current_bizcode==='writebox'){
				//写邮件
				btnEvent.listAddReplace();
			}else{
				//列表页面初始化(收件箱、发件箱、草稿箱、回收站等)
    			private_method.initList({module:current_bizcode});
			}
    	}
    	,
    	
    	clearDetail:function(){
    		var _c =$('#' + rootid +list_c)
    			
    			if(_c){
    				//清除原有内容
    				_c.empty();
    			}
    	}
    	
    	,
    	
    	/**
    	 * 更新按钮
    	 * @param _options.bizcode	业务编码
    	 * @param _options.btnType		按钮类型(list,edit,view)
    	 * @param _options.bizType	业务类型(0:normal,1:flow,2:audit)
    	 * @param _options.container 按钮容器对象
    	 */
    	redrawBtn:function(_options){
    		
    		
    		var _btn ;
    		if(_options && _options.btnType){    			
	    		_btn = bsButton.getBtn(_options);	    				
    		}    		
    		
    		$('#'+_options.container).empty();
    		
    		if(_btn){    			
    			$('#'+_options.container + ' button').unbind('click');    			
    			$('#'+_options.container).append(_btn);    			
    			//绑定按钮事件
    			$('#'+_options.container + ' button').bind('click',private_method.btnClick);
    		}
    		
    	}
    	,
    	
    	reDrawBizBadge:function(){
    		var _el = $('#'+rootid + ' .active span')
    		
    		var grid = $('#'+rootid + ' .dg-list')
    		
    		var _o = grid.datagrid('options')
    		var _cnt = _o.total
    		_el.html(_cnt);
    		
    	}
    	,
    	btnClick:function(e){
    		
    		init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent})
//    		bsMsgServ.msgSlide();
    	}    	
    	,
    	/**
    	 * 初始化写邮件表单页面元素
    	 * @param _options.code		
    	 */
    	initForm:function(_options){
    		
    		
    		var _f = $('#'+rootid + list_c + ' .main-form')
    		
    		//业务id处理			
			if(curent_selectRow ){				
				if(!curent_selectRow.id || curent_selectRow.id==''){
					//id为空,赋值临时id
					var _date = new Date();
					var _id = 'TMP' +current_bizcode+ _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
					curent_selectRow.tmpid = _id;
					curent_selectRow.id = _id;
				}
			}else{
				var _date = new Date();
				var _id = 'TMP' +current_bizcode+ _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
					
				curent_selectRow = {
					tmpid	: _id,
					id		: _id
				}	
			}
			
    		if(window.console){
    			console.log(' issue form length:  '+ _f.length)	
    		}
    		if(_f.length>0){
    			_f.form('load',curent_selectRow);  			
    		}
    		
    	}
    	,
    	
    	/**
    	 * @param _options.id	业务编码
    	 * @param _options.del	删除按钮
    	 */
    	initUpload:function(_options){
			var _data = private_method.getUploadParam();
			bsUtil.initUpload({
    			bizcode 	: moduCode,
    			rootid  	: rootid,
    			containerid : 'archive-container',
    			bizno   	: _data.bizno,
    			del			: true
    		})
			
		}
		,
		/**
		 * 初始化附件信息
		 * @param _options.bizcode	业务编码
		 * @param _options.id	业务id
		 * 
		 */
		initArchive:function(_options){
			var _data = private_method.getUploadParam();
			bsUtil.initArchive({
    			bizcode 	: moduCode,
    			rootid  	: rootid,
    			containerid : 'archive-container',
    			bizno		: _data.bizno,
    			del			: true
    		})
		}
    	,
    	
    	//获取附件上传参数
		getUploadParam:function(){
			//附件参数处理
			
			var _data = {
				bizcode:moduCode
			}
			//业务id处理
			var _idEl = $('#'+rootid +' #id')
			
			var _id
			if(_idEl.length ==1){
				
				
				var _tmpValue = $(_idEl).val()
				
				if(!_tmpValue || _tmpValue==''){
					//id为空,赋值临时id
					var _date = new Date();
					_id = 'TMP' + _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
					var _tmpid = $('#'+rootid +' #tmpid')
					$(_tmpid).val(_id)
					$(_idEl).val(_id)
				}else{
					//id不为空，取id值
					_id = _tmpValue;
				}
			}
			
							
			_data.bizno=_id;	
			
			return _data;
			
		}
		,
		/**
		 * 获取字段必填、可填配置
		 * 
		 */
		getFieldConfig:function(){
			//查询获取模块节点配置			
			var task_config_url = '/'+init.getSite() + field_url;
			var task_param = {}
			
			current_config = init.getBizConfig({bizcode:current_bizcode})
			if(current_config && current_config.fullBizcode){
				task_param.fullBizcode = current_config.fullBizcode;
			}
			
			//当前是否选中行
			if(curent_selectRow && curent_selectRow.task_def_key){
				task_param.activityId=curent_selectRow.task_def_key
			}
			
			$.ajax({
				url:task_config_url,
				data:task_param,
				type : 'post',
				success:private_method.fieldSetting
			})
		}
		
		,
		/**
		 * 设置form字段
		 */
		fieldSetting:function(data){
			
			var archiveEanbled = false;
			
			if(data){						

				var _f = data.editFilter

				var fields

				if(_f && _f.enabled){
					//旧模块流程定义配置模式
					fields = _f.enabled.split(',')
				}else{
					//新模块流程定义配置模式
					if(data.requiredFilter){
						fields = data.requiredFilter.split(',')
					}
				}

				if(fields && (fields.length>0)){
					var rules={},messages={}
					for(var i =0;i<fields.length;i++){
						var _el =$('#' + rootid + ' .main-form #' + fields[i])
						if(_el.length>0){
							 $(_el).addClass('js-required-field')
							 $(_el).removeAttr('readonly')
							 rules[fields[i]]='required';
							 messages[fields[i]]='字段不允许为空';
						}
					}
					ruleConfig = {rules:rules,messages:messages}
//					$($form).form('enableValidation').form('validate')
				}
				
				//可编辑字段
				if(data.editableFilter){
					fields = data.editableFilter.split(',')
					if(fields && (fields.length>0)){
						for(var i=0;i<fields.length;i++){
							var _el =$('#' + rootid + ' .main-form #' + fields[i])
							if(_el.length>0){							 
								$(_el).removeAttr('readonly')							 
							}
						}
					}
				}				
				
				//附件启用标志
				archiveEanbled = data.archiveEnabled
				
			}
			
			
			//附件初始化功能
			var _idEl = $('#'+rootid + ' #id')
			var _id
			if(_idEl.length==1){				
				_id = _idEl.val();
			}
			
			if(_id){
				//编辑页面
				if(archiveEanbled){
					//初始化附件上传
		    		bsUtil.initUpload({
		    			bizcode 	: current_bizcode,
		    			rootid  	: rootid,
		    			containerid : 'archive-container',
		    			bizno   	: _id,
		    			del			: true
		    		})
				}else{
					//初始化附件预览
					var _param = {
		    			bizcode 	: current_bizcode,
		    			rootid  	: rootid,
		    			containerid : 'archive-container',
		    			bizno		: _id
		    		}		    		
					bsUtil.initArchive(_param)
				}
			}else{
				//查询统计页面
				_idEl = $('#'+viewRootid + ' #id')
				if(_idEl.length==1){
					_id = _idEl.val();	
					bsUtil.initArchive({
		    			bizcode 	: current_bizcode,
		    			rootid  	: viewRootid,
		    			containerid : 'archive-container',
		    			bizno		: _id
		    		})
				}	
			}
		}
		,
		validateForm:function(e){
			
			var el = $('#'+rootid + ' .main-form' + ' #name')
    		if(!el.val()){
    			bsMsgServ.warnSlide('邮件标题不能为空');
    			$(e.currentTarget).removeClass('disabled');
    			return 0;
    		}
    		
    		var reciveEnable = false;
    		//验证收件人
    		el = $('#'+rootid + ' .main-form' + ' #reciveUserNames')
    		var _el = el.val();
    		if(_el){
    			reciveEnable = true;    			
    		}
    		//验证收件组
    		el = $('#'+rootid + ' .main-form' + ' #reciveGroupNames')
    		_el = el.val();
    		if(_el){
    			reciveEnable = true;    			
    		}
    		
    		if(!reciveEnable){
    			bsMsgServ.warnSlide('收件人及收件组不能同时为空');
    			$(e.currentTarget).removeClass('disabled');
    			return 0;	
    		}		
			
			return 1
		}
		,
		//获取form数据
		getFormData:function(){
			var _f = $('#'+rootid + ' .main-form')
			var formData = formServ.getDataObj({
						form : _f
					});					
			
			//业务编码
			if(!formData.bizcode){
				
				formData.bizcode = moduCode;
			}
			
			if(formData.id && (formData.id).substring(0,3)=='TMP'){				
				formData.id = null;	
			}
			
			//获取ckeditor值
			var _instance = CKEDITOR.instances[ck_id];
			
			if(_instance){
				var _data = _instance.getData();
				
				if(_data){
					formData.lob_contents = _data;	
				}
			}
			
			return formData;
		}
		,
		triggerNav:function(){
			var _btn = $('#' + rootid + ' .active a')
    		_btn.trigger('click');	
		}
		
		
		,
		
		initCkeditor:function(selectedRecord){
			//初始化ck
			var _instance = CKEDITOR.instances[ck_id]
			if(_instance){ 
			    _instance.destroy(true); 
			}
			CKEDITOR.replace(ck_id,init.getLobConfig());
			
			//初始化ckeditor
			if(selectedRecord  && selectedRecord.id){
				
				$.ajax({
					url		: pageConfig.contentUrl,
					data	: {id:selectedRecord.id},
					async	: false,
					success	: function(data){
						if(data && data.lob_contents){
							_contents = data.lob_contents
						}else{
							_contents = ''	
						}
					}
					,
					failure	: function(){
						bsMsgServ.errorSlide('加载邮件内容出错，请刷新页面后重试.如问题继续存在，请联系系统管理员')
					}
				})				
				
				if(_contents){
					CKEDITOR.instances[ck_id].setData(_contents);
				}
			}
		}
				
	};
	
	//人员及分组选择事件
	var selectEvent = {
		//初始化人员部门选择
    	selectUser:function(e){
    		var _userParam = {
				userids		: rootid +' #reciveUserids',
				usernames	: rootid+' #reciveUserNames',
				//deptid		: rootid +' #org_id',
				//deptname	: rootid+' #org_name',
				//selectType	: 'job',
				//subsystem	:'ethic',
				required 	: true
			}
			deptUserSelect.openPage(_userParam)	
			if(e && e.currentTarget){
				$(e.currentTarget).removeClass('disabled');
			}
    	}
    	,
    	selectGroup:function(e){
    		var _userParam = {
				groupid		: rootid +' #reciveGroupids',
				groupname	: rootid+' #reciveGroupNames',
				//deptid		: rootid +' #org_id',
				//deptname	: rootid+' #org_name',
				//selectType	: 'job',
				//subsystem	:'ethic',
				required 	: true
			}
			groupSelect.openPage(_userParam)	
			if(e && e.currentTarget){
				$(e.currentTarget).removeClass('disabled');
			}
    	}
    		
	}
	var btnEvent = {
		
		//转发
		reWrite	 :function(e){
			//激活写信
			$('#' + rootid + ' #left-nav li').removeClass('active');
    		$('#' + rootid + ' #writeMail li').closest('li').addClass('active');
			//清理form id,接收人员及邮件状态
			var _el = $('#'+rootid + ' #reciveUserids')
			_el.val('');
			
			$('#'+rootid + ' #reciveGroupids').val('');
			$('#'+rootid + ' #reciveUserNames').val('');
			$('#'+rootid + ' #reciveGroupNames').val('');
			
			$('#'+rootid + ' #createTime').val('');
			
			//转发标志赋值
			$('#'+rootid + ' #reWrite').val('1');
			
			//设置当前业务代码
			current_bizcode = 'writebox'
			
			//重置页面按钮
			private_method.redrawBtn({
    			bizcode: current_bizcode,
    			btnType: 'edit',
    			container: detail_btn_container
    		})
			//加载写信按钮
		}
		,
		
		//回复
		reply	 :function(e){
			//激活写信
			$('#' + rootid + ' #left-nav li').removeClass('active');
    		$('#' + rootid + ' #writeMail li').closest('li').addClass('active');
			//设定form id,接收人员及邮件状态
			var _uid = $('#'+rootid + ' #sendids')
			var _uname = $('#'+rootid + ' #sendName')
			
			//邮件名称
			var _mname = $('#'+rootid + ' #name')
			
			$('#'+rootid + ' #reciveUserids').val(_uid.val());
			$('#'+rootid + ' #reciveUserNames').val(_uname.val());
			
			//邮件名称增加回复二字
			$('#'+rootid + ' #name').val('回复: '+_mname.val());
			
			
			$('#'+rootid + ' #createTime').val('');
			
			//邮件内容修改
			var _instance = CKEDITOR.instances[ck_id]
			
			var lobData = '邮件已收到';
			if(_instance){
				lobData += _instance.getData();
				
				_instance.setData(lobData);
			}
			
			
			//转发标志赋值
			//$('#'+rootid + ' #reWrite').val('1');
			
			//设置当前业务代码
			current_bizcode = 'writebox'
			
			//重置页面按钮
			private_method.redrawBtn({
    			bizcode: current_bizcode,
    			btnType: 'edit',
    			container: detail_btn_container
    		})
			//加载写信按钮
		}
		,
		//写信
		writeMail:function(e){
			$('#' + rootid + ' #writebox').trigger('click');
//			btnEvent.listAddReplace(e)
		}
		,
		//当前页新增
		listAddReplace:function(e){
    		
			//清除缓存数据
			curent_selectRow = null;
    		var _url =  pageConfig.editUrl;
    		bsButtonEvent.add({
    			url:_url,
    			container: rootid  + ' .list-detail',
    			formInit:null,
    			evt:e
    		})
    		
    		private_method.redrawBtn({
    			bizcode: current_bizcode,
    			btnType: 'edit',
    			container: detail_btn_container
    		})
    		
    		if(e && e.currentTarget){
    			$(e.currentTarget).removeClass('disabled')	
    		}
    	}
    	
    	,
    	//当前页编辑
    	listEditReplace:function(e){
    		var _record = $('#' + rootid + ' .dg-list').datagrid('getSelections');
    		
    		if(_record.length==1){
	    		var _url = pageConfig.editUrl;
	    		bsButtonEvent.add({
	    			url:_url,
	    			container: rootid + ' .list-detail',
	    			formInit:null,
	    			data:_record,
	    			evt:e
	    		})	 
	    		
	    		private_method.redrawBtn({
	    			bizcode: current_bizcode,
	    			btnType: 'edit',
	    			container: detail_btn_container
	    		})
	    		//暂存当前选中行
	    		curent_selectRow = _record[0];
    		}else{
    			if(_record.length>1){
    				bsMsgServ.warnSlide('您选择了多条记录，请鼠标双击想编辑的行.')	
    			}else{
    				bsMsgServ.warnSlide('请选择想操作的行')	
    			}
    			$(e.currentTarget).removeClass('disabled');
    		}
    		
    		
    	}
    	,
    	
    	listRealDel:function(e){
    		btnEvent.executeDel(pageConfig.delUrl,e);
    	}
    	,
    	listDel:function(e){
    		btnEvent.executeDel(pageConfig.delStatusUrl,e);
    	}
    	,
    	executeDel:function(url,e){
    		var _grid = $('#' + rootid + ' .dg-list')
    		var _record = _grid.datagrid('getSelections');
    		if(_record.length>0){
    			datagridServ.delRow({
    				url: url,
    				target:_grid,
    				callback:function(data){
    					if(data){
    						if(data.error){
    							var _param = {type:'list'}
    							switch(data.error){
    								case 'data':
    									_param.code='data'
		    							
	    								break;
	    							case 'audit':
	    								_param.code='audit'
	    								break;
    							}
    							bsMsgServ.warnSlide(_param);
    							
    						}else{
    							bsMsgServ.msgSlide(
	    							codeDef.getConfig({
	    								type:'operate',
	    								code:'success'
	    							})	
    							)
    							bsUtil.updateIssueBadge();
    						}
    					}
    				}
    			})
    		}else{
    			bsMsgServ.warnSlide('请选择想操作的行');
    		}
    		$(e.currentTarget).removeClass('disabled');
    	}
    	,
    	//弹窗编辑
    	listEditPop:function(e){
    		
    		
    	}
    	,
    	//取消编辑
    	editCancelReplace:function(){
    		private_method.triggerNav();
    	}
    	,
    	
    	//发送
    	editRedo : function(e){
    		//验证邮件标题
    		
    		if(private_method.validateForm(e)){
    		
    		
    			btnEvent.save({url:pageConfig.sendUrl,evt:e,refresh:true})
    		}
    		
    	}
    	,
    	//存草稿
    	editDraft : function(e){
    		btnEvent.save({url:pageConfig.saveUrl,evt:e})
    	}
    	,
    	/**
    	 * @param _options.url	执行url
    	 * @param _options.evt	按钮事件
    	 * @param _options.refresh
    	 */
		save : function(_options) {
			
			if(window.console){
				console.log(' save fired ...')	
			}
			var _f = $('#'+rootid + ' .main-form')	
			
			var formData = private_method.getFormData();		
			
			$.ajax({
				url			: _options.url,
				data		: $.toJSON(formData),
				contentType : 'appliction/json;charset=utf-8',
				type		: 'post',
				dataType	: 'json',
				success		: function(data){
					if(data){						
						
						//刷新form
						$('#'+rootid + ' .main-form').form('load',data);
						
					}
					bsMsgServ.msgSlide(codeDef.getConfig({
						type:'operate',
						code:'success'
					}))
					
					bsUtil.updateIssueBadge();
					if(_options){
						if(_options.evt)
							$(_options.evt.currentTarget).removeClass('disabled');
						
							//判断是否刷新页面
						if(_options.refresh){
							btnEvent.listAddReplace();	
						}
					}
						
					
				}
				,
				failure		: function(){
					bsMsgServ.errorSlide(codeDef.getConfig({
						type:'operate',
						code:'error'
					}))
					if(_options && _options.evt)
						$(_options.evt.currentTarget).removeClass('disabled');
				}
			})
							
		}
		
		
	}
	
	var public_method = {
		initList:function(){
			private_method.initNav();
		}
		,
		/**
		 * 初始化编辑页面
		 */
		initEdit:function(_options){
			private_method.initEdit(_options)
		}
		,
		destory:function(){
			//取消签名绑定事件
			var _el = $('#'+rootid + ' .main-form .sign-user')
			_el.unbind('click');
		}
	}
	
	return public_method;
		
    
})
